<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/1/15
 * Time: 13:52
 */

namespace Resource\Service;
use Think\Model;

class MessageService extends Model
{
    protected $trueTableName = 'wz_message';
    /**
     * @param $params
     * @return array|bool
     */
    public function addMessage($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $item_id = D('Resource/Message')->addMessage($params);
        if($item_id) {
            return ['id'=>$item_id];
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getMessageInfo($params, $fields='') {
        $dataInfo = D('Resource/Message')->getMessageInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchMessageList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['uid']) && $data['uid'] = $params['uid'];
        isset($params['customer_id']) && $data['customer_id'] =$params['customer_id'];
        isset($params['status']) && $data['status'] =$params['status'];
        isset($params['_string']) && $data['_string'] =$params['_string'];

        //查询项目总数
        $total = D('Resource/Message')->getMessageCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询项目列表
        $dataList = D('Resource/Message')->getMessageList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['msgList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateMessageInfo($params) {
        return D('Resource/Message')->updateMessageInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllMessageList($params){
        $dataInfo = D('Resource/Message')->getAllMessageList($params);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return int
     */
    public function getMessageCount($params){
        $count = D('Resource/Message')->getMessageCount($params);
        return intval($count);
    }
}